package com.qimei.onerice.dao.impl;

import com.qimei.onerice.dao.OrderDao;
import com.qimei.onerice.entity.Order;
import com.qimei.onerice.until.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDaoImpl implements OrderDao {

	private static final String INSERT_ORDER = "INSERT INTO orders (order_id, customer_id, order_time, order_status, total_amount) VALUES (?, ?, ?, ?, ?)";
	private static final String SELECT_ORDER_BY_ID = "SELECT * FROM orders WHERE order_id = ?";
	private static final String SELECT_ORDERS_BY_CUSTOMER_ID = "SELECT * FROM orders WHERE customer_id = ?";
	private static final String SELECT_ALL_ORDERS = "SELECT * FROM orders";
	private static final String UPDATE_ORDER = "UPDATE orders SET customer_id = ?, order_time = ?, order_status = ?, total_amount = ? WHERE order_id = ?";
	private static final String DELETE_ORDER = "DELETE FROM orders WHERE order_id = ?";
	private static final String GET_CURRENT_ORDER = "SELECT * FROM orders WHERE order_status = '未支付'";

	@Override
	public int addOrder(Order order) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(INSERT_ORDER)) {
			preparedStatement.setInt(1, order.getOrderId());
			preparedStatement.setInt(2, order.getCustomerId());
			preparedStatement.setDate(3, new java.sql.Date(order.getOrderTime().getTime()));
			preparedStatement.setString(4, order.getOrderStatus());
			preparedStatement.setDouble(5, order.getTotalAmount());
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	@Override
	public Order getOrderById(int orderId) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_ORDER_BY_ID)) {
			preparedStatement.setInt(1, orderId);
			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				if (resultSet.next()) {
					return createOrderFromResultSet(resultSet);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return null;
	}

	@Override
	public Order getCurrentOrder() {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(GET_CURRENT_ORDER)) {
			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				if (resultSet.next()) {
					return createOrderFromResultSet(resultSet);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return null;
	}

	@Override
	public List<Order> getOrdersByCustomerId(int customerId) {
		List<Order> orders = new ArrayList<>();
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_ORDERS_BY_CUSTOMER_ID)) {
			preparedStatement.setInt(1, customerId);
			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				while (resultSet.next()) {
					orders.add(createOrderFromResultSet(resultSet));
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return orders;
	}

	@Override
	public List<Order> getAllOrders() {
		List<Order> orders = new ArrayList<>();
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_ALL_ORDERS);
		     ResultSet resultSet = preparedStatement.executeQuery()) {
			while (resultSet.next()) {
				orders.add(createOrderFromResultSet(resultSet));
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return orders;
	}

	@Override
	public int updateOrder(Order order) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_ORDER)) {
			preparedStatement.setInt(1, order.getCustomerId());
			preparedStatement.setDate(2, new java.sql.Date(order.getOrderTime().getTime()));
			preparedStatement.setString(3, order.getOrderStatus());
			preparedStatement.setDouble(4, order.getTotalAmount());
			preparedStatement.setInt(5, order.getOrderId());
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	@Override
	public int deleteOrder(int orderId) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(DELETE_ORDER)) {
			preparedStatement.setInt(1, orderId);
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	// Helper 方法，从 ResultSet 中创建 Order 对象
	private Order createOrderFromResultSet(ResultSet resultSet) throws SQLException {
		Order order = new Order();
		order.setOrderId(resultSet.getInt("order_id"));
		order.setCustomerId(resultSet.getInt("customer_id"));
		order.setOrderTime(resultSet.getDate("order_time"));
		order.setOrderStatus(resultSet.getString("order_status"));
		order.setTotalAmount(resultSet.getDouble("total_amount"));
		return order;
	}
}
